package com.pengpeng.server.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.pengpeng.server.database.Database;
import com.pengpeng.server.database.DatabaseFactory;
import com.pengpeng.server.database.ISqlObj;
import com.pengpeng.server.entity.ServerError;

public class ServerErrorDao{

	private DatabaseFactory databasefactory;
		
	public List<ServerError> query()
	{
		RowMapper<ServerError> mapper=new RowMapper<ServerError>()
		{
			@Override
			public ServerError mapRow(ResultSet rs, int arg1) throws SQLException 
			{
				
				String errorid;
				String phonenum;
				String errortype;                   
				String appname; 
				String versionnum;
				String funcname;     
			    String errorcontent;
			    String errortimestr;
				Date   errortime;
				
				errorid=rs.getString("errorid");
				phonenum=rs.getString("phonenum");
				errortype=rs.getString("errortype");                   
				appname=rs.getString("appname"); 
				versionnum=rs.getString("versionnum");
				funcname=rs.getString("funcname");     
			    errorcontent=rs.getString("errorcontent");       
				errortimestr=rs.getString("errortime");
		
				SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					errortime= dateformat.parse(errortimestr);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					errortime=new Date();
				}
					
				ServerError servererror=new ServerError();
				servererror.setErrorid(errorid);
				servererror.setPhonenum(phonenum);
				servererror.setErrortype(errortype);
				servererror.setAppname(appname);
				servererror.setVersionnum(versionnum);
				servererror.setFuncname(funcname);
				servererror.setErrorcontent(errorcontent);
				servererror.setErrortime(errortime);
				
				return servererror;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryServerError");
	    try {  
	    	
	    	List<ServerError> servererrorlist=jdbctemplate.query(sqlstr, mapper);
		    return  servererrorlist; 
	        } catch (EmptyResultDataAccessException e) {  
	          return null;  
	    } 
	}//query
	
	
	public boolean update(ServerError servererror)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		String errorid=servererror.getErrorid();
		String phonenum=servererror.getPhonenum();
		String errortype=servererror.getErrortype();                   
		String appname=servererror.getAppname(); 
		String versionnum=servererror.getVersionnum();
		String funcname=servererror.getFuncname();     
	    String errorcontent=servererror.getErrorcontent();
	    Date   errortime=servererror.getErrortime();
	    
	    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String errortimestr=dateformat.format(errortime);
			
		Object[] params = new Object[] {errorid,phonenum,errortype,appname,versionnum,funcname,errorcontent,errortimestr,errorid};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("updateServerError");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}
	
	public boolean insert(ServerError servererror)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		String errorid=servererror.getErrorid();
		String phonenum=servererror.getPhonenum();
		String errortype=servererror.getErrortype();                   
		String appname=servererror.getAppname(); 
		String versionnum=servererror.getVersionnum();
		String funcname=servererror.getFuncname();     
	    String errorcontent=servererror.getErrorcontent();
	    Date   errortime=servererror.getErrortime();
	    
	   
	    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String errortimestr=dateformat.format(errortime);
			
	    if(errorid.equals(""))
	    {
	    	dateformat = new SimpleDateFormat("yyyyMMddHHmmssms");
	    	errorid=phonenum+errortype+dateformat.format(new Date());
	    }
	    
		Object[] params = new Object[] {errorid,phonenum,errortype,appname,versionnum,funcname,errorcontent,errortimestr};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("insertServerError");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}
	public boolean delete(ServerError servererror)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		Object[] params = new Object[] {servererror.getErrorid()};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("deleteServerError");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}//delete
	public DatabaseFactory getDatabasefactory() {
		return databasefactory;
	}
	public void setDatabasefactory(DatabaseFactory databasefactory) {
		this.databasefactory = databasefactory;
	}

}
